// @flow
import React, { Component } from 'react'
import { Modal, Form, Input, Row, Col, Switch } from 'antd'
// const Option = Select.Option
import R from 'ramda'
// import universalFetch, { handleFetchError } from 'store/modules/fetch'
const FormItem = Form.Item
const formItemLayout = {
  labelCol: {
    xs: { span: 24 },
    sm: { span: 6 }
  },
  wrapperCol: {
    xs: { span: 24 },
    sm: { span: 14 }
  }
}

type Props = {
  form: Object,
  removeNode: Function,
  data: Object,
  index: number,
  changeContact: Function,
  contactData: Array<Object>
}

type States = {
  visible: boolean,
  confirmLoading: boolean
}
class EditContact extends Component {
  props: Props
  state: States
  closeModal: Function
  handleSubmit: Function
  constructor (props: Props) {
    super(props)
    this.state = {
      visible: true,
      confirmLoading: false
    }
    this.closeModal = this.closeModal.bind(this)
    this.handleSubmit = this.handleSubmit.bind(this)
  }

  closeModal () {
    this.setState({
      visible: false
    })
    this.props.removeNode()
  }

  handleSubmit () {
    const form = this.props.form
    form.validateFields((err, fieldsValue) => {
      if (err) {
        return false
      }
      this.setState({
        confirmLoading: true
      })
      const { index, contactData } = this.props
      let importance = 'secondary'
      if (fieldsValue.importance) {
        importance = 'primary'
      }
      const values = {
        ...fieldsValue,
        importance: importance
      }
      if (importance === 'primary') {
        contactData.map((d) => {
          d['importance'] = 'secondary'
        })
      }
      let arr = R.clone(contactData)
      arr[index] = values
      this.props.changeContact(arr)
      this.setState({
        confirmLoading: false
      }, () => {
        this.closeModal()
      })
    })
  }
  render () {
    const { getFieldDecorator } = this.props.form
    const { data } = this.props
    const { visible, confirmLoading } = this.state
    const importance = data.importance === 'primary' ? true : false
    return (
      <Modal
        title='编辑联系人'
        width={800}
        visible={visible}
        onCancel={this.closeModal}
        confirmLoading={confirmLoading}
        onOk={this.handleSubmit}
        >
        <Form style={{ height: 350, overflow: 'auto' }}>
          <Row>
            <Col span={12}>
              <FormItem {...formItemLayout} label='姓名' hasFeedback>
                {getFieldDecorator('name', {
                  rules: [
                    { required: true, message: '请填写姓名' }
                  ],
                  initialValue: data.name || ''
                })(
                  <Input placeholder='请填写姓名' />
                )}
              </FormItem>
              <FormItem {...formItemLayout} label='手机号' hasFeedback>
                {getFieldDecorator('phoneNum', {
                  rules: [
                    { required: true, message: '请填写手机号' }
                  ],
                  initialValue: data.phoneNum || ''
                })(
                  <Input placeholder='请填写手机号' />
                )}
              </FormItem>
              <FormItem {...formItemLayout} label='默认联系人' hasFeedback>
                {getFieldDecorator('importance', {
                  initialValue: importance
                })(
                  <Switch defaultChecked={importance} checkedChildren='是' unCheckedChildren='否' />
                )}
              </FormItem>
              <FormItem {...formItemLayout} label='微信' hasFeedback>
                {getFieldDecorator('wechat', {
                  initialValue: data.wechat || ''
                })(
                  <Input placeholder='请填写微信' />
                )}
              </FormItem>
              <FormItem {...formItemLayout} label='公司' hasFeedback>
                {getFieldDecorator('companyName', {
                  initialValue: data.companyName || ''
                })(
                  <Input placeholder='请填写公司' />
                )}
              </FormItem>
              <FormItem {...formItemLayout} label='部门' hasFeedback>
                {getFieldDecorator('departmentName', {
                  initialValue: data.departmentName || ''
                })(
                  <Input placeholder='请填写部门' />
                )}
              </FormItem>
            </Col>
            <Col span={12}>
              <FormItem {...formItemLayout} label='邮箱' hasFeedback>
                {getFieldDecorator('email', {
                  rules: [
                    { required: true, message: '请填写邮箱' }
                  ],
                  initialValue: data.email || ''
                })(
                  <Input placeholder='请填写邮箱' />
                )}
              </FormItem>
              <FormItem {...formItemLayout} label='QQ' hasFeedback>
                {getFieldDecorator('qqNum', {
                  initialValue: data.qqNum || ''
                })(
                  <Input placeholder='请填写QQ' />
                )}
              </FormItem>
              <FormItem {...formItemLayout} label='单位电话' hasFeedback>
                {getFieldDecorator('businessNum', {
                  initialValue: data.businessNum || ''
                })(
                  <Input placeholder='请填写单位电话' />
                )}
              </FormItem>
              <FormItem {...formItemLayout} label='住宅电话' hasFeedback>
                {getFieldDecorator('homeNum', {
                  initialValue: data.homeNum || ''
                })(
                  <Input placeholder='请填写住宅电话' />
                )}
              </FormItem>
              <FormItem {...formItemLayout} label='传真' hasFeedback>
                {getFieldDecorator('fax', {
                  initialValue: data.fax || ''
                })(
                  <Input placeholder='请填写传真' />
                )}
              </FormItem>
              <FormItem {...formItemLayout} label='备注' hasFeedback>
                {getFieldDecorator('remark', {
                  initialValue: data.remark || ''
                })(
                  <Input placeholder='请填写备注' />
                )}
              </FormItem>
              <FormItem {...formItemLayout} label='是否是自己添加' hasFeedback style={{ height:0, overflow:'hidden' }}>
                {getFieldDecorator('isSelf', {
                  initialValue:'yes'
                })(
                  <Input />
                )}
              </FormItem>
            </Col>
          </Row>
        </Form>
      </Modal>
    )
  }
}

export default Form.create()(EditContact)
